package com.lwq.project.controller.role;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lwq.project.model.Role;
import com.lwq.project.model.Role2mune;
import com.lwq.project.model.custom.CustomMune;
import com.lwq.project.service.MuneService;
import com.lwq.project.service.Role2muneService;
import com.lwq.project.service.RoleService;
import com.lwq.project.utils.UtilsTools;
import com.lwq.project.utils.page.Pages;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private MuneService muneService;
	@Autowired
	private Role2muneService role2muneService;
	
	@RequestMapping("/roleView.action")
	public String init(Model model){
		return "main/role/roleView";
	}
	@RequestMapping("/roleTable.action")
	public String initList(Model model,Pages pages){
		Map<String,Object> map = new HashMap<String,Object>() ;
		map.put("pages", pages);
		List<Role> roleList = roleService.getPageRole(map);
		int count = roleService.countRole();
		pages.setNumberCount(count);
		model.addAttribute("roleList", roleList);
		model.addAttribute("pages", pages);
		return "main/role/roleTable";
	}
	
	@RequestMapping("/roleEdit.action")
	public String initEdit(String rId,Model model){
		if(!StringUtils.isBlank(rId)){
			model.addAttribute("role", roleService.getRoleById(rId));
		}
		return "main/role/roleEdit";
	}
	
	@RequestMapping("/roleSave.action")
	@ResponseBody
	public String initSave(String rName,String rDescribe,String rGrade,String rId){
		Role role = new Role();
		role.setrDescribe(rDescribe);
		role.setrName(rName);
		role.setrGrade(rGrade);
		if(StringUtils.isBlank(rId)){
			rId=UtilsTools.getPrimary();
			role.setrId(rId);
			roleService.insertRole(role);
		}else{
			role.setrId(rId);
			roleService.updateRole(role);
		}
		return "success";
	}
	@RequestMapping("/roleDel.action")
	@ResponseBody
	public String initDelete(@RequestParam(required = false,value = "rIds[]")  String[] rIds){
		roleService.deleteRolesById(rIds);
		return "success";
	}
	
	@RequestMapping("/rolePower.action")
	public String initPower(String rId,Model model){
		model.addAttribute("rId", rId);
		return "main/role/rolePower";
	}

	@RequestMapping("/role2mune.action")
	public String initRoleMune(String rId,String mGrade,Model model){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("rId", rId);
		map.put("mGrade", mGrade);
		
		model.addAttribute("muneList", muneService.getMuneByRidMgrade(map));
		model.addAttribute("muneNList", muneService.getMuneByNRidMgrade(map));
		return "main/role/role2mune";
	}
	
	@RequestMapping(value="/roleAddmune.action",produces="text/html;charset=UTF-8")//
	@ResponseBody
	public String addMune(String rId,String mId,String mGrade){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("rId", rId);
		map.put("mId", mId);
		if(!StringUtils.equals(mGrade, "1")){
			int count = role2muneService.checkSecondMune(map);
			if(count==0){
				CustomMune cm = muneService.getMuneById(mId);
				String name = cm.getParentName();
				return name;
			}
		}
		Role2mune rm = new Role2mune();
		rm.setRmId(UtilsTools.getPrimary());
		rm.setrId(rId);
		rm.setmId(mId);
		role2muneService.addMRoleMune(rm);
		return "success";
	}
	@RequestMapping(value="/roleDelmune.action",produces="text/html;charset=UTF-8")//
	@ResponseBody
	public String delMune(String rId,String mId,String mGrade){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("rId", rId);
		map.put("mParent", mId);
		map.put("mId", mId);
		if(StringUtils.equals(mGrade, "1")){
			List<Map<String,Object>> list = role2muneService.getSecondMune(map);
			if(list.size()!=0){
				StringBuilder sb = new StringBuilder();
				for (Map<String, Object> map2 : list) {
					sb.append(map2.get("m_name")+"、");
				}
				sb.deleteCharAt(sb.length()-1);
				return sb.toString();
			}
		}
		role2muneService.delRoleMune(map);
		return "success";
	}
}
